﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using You.Niao.User.Model;
using System.Reflection;
using You.Niao.User.IDAL;
using System.Data.SqlClient;
using DBUtility;
using Common.Base;
using You.Niao.Pic.Model;
using System.Data;

namespace You.Niao.User.DAL
{
    public class User :  SqlConn,IUser
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Users UserLogin(Users model)
        {
            Users m= new Users();
            SqlParameter[] param = {
                                       new SqlParameter("@Email",model.Email),
                                       new SqlParameter("@Pwd",model.Pwd)
                                   };
            SqlDataReader sdr = DbHelperSQL.ExecuteReader(UserConn, "SELECT UserID,NickName,Status,Avatar,LetterNumber,MessageNumber FROM Users u WHERE u.Email=@Email AND u.Pwd=@Pwd ", param);
            while (sdr.Read())
            {
                m.UserID = sdr.IsDBNull(0) ? 0 : Convert.ToInt32(sdr[0]);
                m.NickName = sdr.IsDBNull(1) ? "" : sdr[1].ToString();
                m.Status = sdr.IsDBNull(2) ? 0 :Convert.ToInt16(sdr[2]);
                m.Avatar = sdr.IsDBNull(3) ? " /images/d1.jpg" : sdr[3].ToString();
                m.LetterNumber = sdr.GetInt32(4);
                m.MessageNumber = sdr.GetInt32(5);
            }
            sdr.Dispose();
            return m;
        }
        public bool QQLogin(Users user)
        {
            return false;
        }
        public bool xinlangLogin(Users user)
        {
            return false;
        }

       /// <summary>
       /// 创建用户
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
        public bool CreateUser(Users model)
        {
            SqlParameter[] param = {
                                       new SqlParameter("@Email",model.Email),
                                       new SqlParameter("@NickName",model.NickName),
                                       new SqlParameter("@Pwd",model.Pwd),
                                       new SqlParameter("@Secret",model.Secret)
                                   };
            int row=0;
            return DbHelperSQL.RunProcedure(UserConn, "AddUser", param, out row) > 0 ? true : false;
        }

        public  bool CheckNickName(string NickName)
        {
            SqlParameter[]param={new SqlParameter("@NickName",NickName)};
            return   DbHelperSQL.Exists(UserConn, "SELECT COUNT(1) FROM users u WHERE u.NickName=@NickName",param);
         }

        public bool   CheckEmail(string Email)
        {
            SqlParameter[] param = { new SqlParameter("@Email", Email) };
            return DbHelperSQL.Exists(UserConn, "SELECT COUNT(1) FROM users u WHERE u.Email=@Email", param);
        }

        /// <summary>
        /// 验证状态
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
       public  Users UserVerification(string code)
       {
            Users user = new Users();
            SqlParameter[]param={new SqlParameter("@code",code)};
            SqlDataReader sdr = DbHelperSQL.ExecuteReader(UserConn, "SELECT UserID,NickName,isnull(Status,0) FROM Users  WHERE Secret=@code", param);
            while (sdr.Read())
            {
              user.UserID = sdr.IsDBNull(0) ? 0 : sdr.GetInt32(0);
              user.NickName = sdr.IsDBNull(1) ? "" : sdr[1].ToString();
              user.Status = sdr.IsDBNull(2) ? 0 : Convert.ToInt16(sdr[2]);
            }
            sdr.Dispose();
            if (user != null)
            {
              if (user.Status == 0)
              {
                DbHelperSQL.Query(UserConn,string.Format("UPDATE Users SET STATUS=1  where UserID={0}", user.UserID));
              }
            }
            return user;
       }

        /// <summary>
        /// 获取个人信息
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
       public Users GetUserInfo(int UserID)
       { 
           Users u=new Users();
           string sql = "select u.Sofa,u.Fraternity,u.Companion,u.Tourcount,u.Recommend,u.NickName,u.Avatar,u.Lable,u.Approve,u.LetterNumber,u.MessageNumber,u.Sex,u.Province,u.City,u.Area,u.Birthday from Users u where u.UserID =@UserID";
           SqlParameter[] pars ={new SqlParameter("@UserID",UserID)};
           SqlDataReader dr = DbHelperSQL.ExecuteReader(UserConn,sql,pars);
           while(dr.Read())
           {
               u.Sofa = dr.GetInt32(0);
               u.Fraternity = dr.GetInt32(1);
               u.Companion = dr.GetInt32(2);
               u.Tourcount = dr.GetInt32(3);
               u.Recommend = dr.IsDBNull(4) ? "" : dr[4].ToString();
               u.NickName = dr[5].ToString();
               u.Avatar = dr.IsDBNull(6) ? "" : dr[6].ToString();
               u.Lable = dr.IsDBNull(7) ? "" : dr[7].ToString();
               u.Approve = dr.GetInt16(8);
               u.LetterNumber = dr.GetInt32(9);
               u.MessageNumber = dr.GetInt32(10);
               u.Sex = dr.GetInt16(11);
               u.Province = dr.IsDBNull(12) ? "" : dr[12].ToString();
               u.City = dr.IsDBNull(13) ? "" : dr[13].ToString();
               u.Area = dr.IsDBNull(14) ? "" : dr[14].ToString();
               u.Birthday =dr.IsDBNull(15) ? DateTime.Now.AddYears(-26):dr.GetDateTime(15);
               u.UserID = UserID;///返回给网页用户ID
           }
           dr.Dispose();
           return u;
       }

       /// <summary>
       /// 更新一条数据
       /// </summary>
       public bool Update(Users users)
       {
           string sql = string.Format("update Users set NickName=@NickName,Sex={0},Province=@Province,City=@City,Area=@Area,Birthday=@Birthday,Signature=@Signature where UserID={1}", users.Sex,users.UserID);
           SqlParameter[] parameters = {
					new SqlParameter("@NickName",users.NickName),
					new SqlParameter("@Province",users.Province),
					new SqlParameter("@City", users.City),
					new SqlParameter("@Area", users.Area),
					new SqlParameter("@Birthday",users.Birthday),
					new SqlParameter("@Signature",users.Signature)
                                       };
           int rows=  DbHelperSQL.ExecuteSql(UserConn,sql,parameters);
           if (rows > 0)
           {
               return true;
           }
           else return false;
           
       }
    }
}